package com.kaifamiao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

public class SeatProvider implements ProviderMethodResolver {

    //根据楼层，教室，座位定位查询
    public String selectOneSeat(@Param("sno")int sno, @Param("fno")int fno, @Param("rno")String rno) {

        return new SQL(){{
            SELECT("f.fno,f.fstate,r.rno,r.rstate,s.sno,s.sstate");
            FROM("seat s");
            LEFT_OUTER_JOIN("room r on s.rno = r.rno");
            LEFT_OUTER_JOIN("floor f on r.fno = f.fno");

            if(sno != 0){
                WHERE("s.sno = #{sno}");
            }
            if(fno != 0){
                WHERE("f.fno = #{fno}");
            }
            if(rno != null){
                WHERE("r.rno = #{rno}");
            }

        }}.toString();

    }

    //根据楼层，教室定位查询出座位状态
    public String selectSeat(@Param("fno")int fno, @Param("rno")String rno) {

        return new SQL(){{
            SELECT("f.fno,f.fstate,r.rno,r.rstate,s.sno,s.sstate");
            FROM("seat s");
            LEFT_OUTER_JOIN("room r on s.rno = r.rno");
            LEFT_OUTER_JOIN("floor f on r.fno = f.fno");

            if(fno != 0){
                WHERE("f.fno = #{fno}");
            }
            if(rno != null){
                WHERE("r.rno = #{rno}");
            }

        }}.toString();

    }


    //根据楼层，教室定位查询出所有已选座位的座位号
    public String oKSeat(@Param("fno")int fno, @Param("rno")String rno) {

        return new SQL(){{
            SELECT("s.sno");
            FROM("seat s");
            LEFT_OUTER_JOIN("room r on s.rno = r.rno");
            LEFT_OUTER_JOIN("floor f on r.fno = f.fno");

            if(fno != 0){
                WHERE("f.fno = #{fno}");
            }
            if(rno != null){
                WHERE("r.rno = #{rno}");
            }
            WHERE("s.sstate = 1");

        }}.toString();

    }

}
